"""
Case Type   : WEAK_PASSWORD_DICTIONARY
Case Name   : 初始用户向系统表插入弱口令
Create At   : 2022/08/12
Owner       : @zou_jialiang050
Description :
    1.初始用户向系统表gs_global_config添加弱口令
    2.查看系统表
    3.清理环境
Expect      :
    1.成功
    2.返回新插入弱口令
    3.成功
History     :
"""

import os
import unittest
from yat.test import macro
from yat.test import Node
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class Confirguration(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.node_sh = Node(node='PrimaryDbUser')
        self.Con = Constant()

    def test_alter_global_configuration(self):
        step = 'step1:初始用户向系统表gs_global_config插入弱口令 ' \
               'expect:成功'
        self.logger.info(step)
        alter_configuration = self.primary_sh.execut_db_sql(
            f'''create weak password dictionary ('test_123A'),
            ('test1_234W');''',
            sql_type=f" -U {self.node_sh.ssh_user} -W "
            f"'{macro.PASSWD_REPLACE}'")
        self.logger.info(alter_configuration)
        self.assertIn('CREATE WEAK PASSWORD DICTIONARY', alter_configuration,
                      '执行失败' + step)

        step = 'step2:查看系统表 expect:返回新插入弱口令'
        self.logger.info(step)
        select_configuration = self.primary_sh.execut_db_sql(
            f'''select * from gs_global_config;''')
        self.logger.info(select_configuration)
        self.assertIn('test_123A', select_configuration, '执行失败' + step)
        self.assertIn('test1_234W', select_configuration, '执行失败' + step)

    def tearDown(self):
        step = 'step3:清理环境 expect:成功'
        self.logger.info(step)
        drop_weak = self.primary_sh.execut_db_sql(
            f'''drop weak password dictionary;''',
            sql_type=f" -U {self.node_sh.ssh_user} -W "
            f"'{macro.PASSWD_REPLACE}'")
        self.logger.info(drop_weak)
        self.assertIn('DROP WEAK PASSWORD DICTIONARY', drop_weak, '执行失败' +
                      step)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
